/*
	功能:  使用  硬件i2c/硬件i2c+dma 驱动0.96寸oled屏
	协议:   i2c
	工具:  keil5 + 官方标准库  编码utf-8
	硬件:  stm32f103系列     oled(四脚i2c协议) SD1306驱动
	资料:   https://blog.csdn.net/yougeng123/article/details/103734303
	资料:   https://blog.csdn.net/weixin_44457994/article/details/114768659
	资料:   https://gitee.com/jiezhuonew/
	
	引脚: SCL -> B6    SDA->B7
	选择字体 12/16/24
*/

#include "stm32f10x.h"
#include "Delay.h"
#include "oled.h"

extern char runModeStr[50];

//帧率计数器相关的变量 比较笼统帧数显示不需要可以去掉
uint32_t msCount = 0;
uint32_t fps = 0;
uint32_t fps_count = 0;

//一毫秒中断
void TIM2_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
        msCount++;
			  if(msCount >= 1000){
					  msCount = 0;
					  fps = fps_count;
					  fps_count = 0;
						//显示帧数
						OLED_ShowINT(95, 48, fps, 16, OLED_DISPLAYCHAR_REVERSE);
				}
    }
}

void TIM2_Configuration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    TIM_TimeBaseStructure.TIM_Period = 7200 - 1; // 72MHz / 7200 = 10kHz
    TIM_TimeBaseStructure.TIM_Prescaler = 10 - 1; // 10kHz / 10 = 1kHz (1ms)
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM2, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}


int main(void)
{
		//初始化oled
		OLED_Init();
	
		//清下屏幕
		OLED_Clear();
	
		TIM2_Configuration();

		//划线
		u8 i;
		for(i = 0; i < 128; i += 10)
		{
			OLED_DrawLine(0,0,128,128-i,1);
			OLED_DrawLine(128,0,0,128-i,1);
			OLED_Refresh_OneTime();
		}
	
		//显示单个字符 可以显示12,16,24字号
		OLED_ShowChar(0, 0, 'A', 16,OLED_DISPLAYCHAR_REVERSE);
		OLED_ShowChar(120, 0, 'B', 16, OLED_DISPLAYCHAR_REVERSE);
		OLED_ShowChar(0, 50, 'C', 16, OLED_DISPLAYCHAR_REVERSE);
		
		//显示字符串
		OLED_ShowString(20, 20, (u8*)runModeStr, 16, OLED_DISPLAYCHAR_REVERSE);
		//OLED_ShowString(20, 20, (u8*)"helo world", 16, OLED_DISPLAYCHAR_REVERSE);
		
		//显示中文
		OLED_Show16X16oneCN(20, 38, "哈距离", OLED_DISPLAYCHAR_REVERSE);
		
		//在右下角循环数字
		/*for(int i=0; i<3000; i++){
			OLED_ShowINT(95, 48, i, 16, OLED_DISPLAYCHAR_REVERSE);
			OLED_Refresh_OneTime();
		}*/
		
		while(1){
			//显示图标 二极管/实心圆/各种图片
			OLED_ShowImage(0, 20, 32, 32, dinosaurBMP1);
			OLED_Refresh_OneTime();
			Delay_ms(8); 

			//显示图标 二极管/实心圆/各种图片
			OLED_ShowImage(0, 20, 32, 32, dinosaurBMP2);
			//刷新数据到屏幕上
			OLED_Refresh_OneTime();
			Delay_ms(8); 
			
			fps_count++;
		}
}